bin_path	:= ../bin
# Makefile 绝对路径
# mkf_path := $(abspath $(lastword $(MAKEFILE_LIST)))
# mkf_abpath := $(patsubst %/Makefile, %/, dir $(mkf_abpath))
mkf_abpath	:= $(patsubst %Makefile, %, $(abspath $(MAKEFILE_LIST)))
# 搜索所有需要链接的文件
# srcs	:= $(wildcard */*.c) $(wildcard *.c)
srcs	:= $(wildcard */*.c)
objs	:= $(patsubst %.c, %.o, $(srcs))
bins	:= $(patsubst %.c, %.out, $(wildcard *.c))
binAbPath	:= $(mkf_abpath)../bin 

# 保留所有中间文件
.SECONDARY:

# 首个目标 ”all”随便定义
all: dir $(bins)

dir:
	mkdir -p ${binAbPath}

%.out: %.o $(objs)
	gcc -o $(bin_path)/$* $^

%.o: %.c
	gcc -c $< -o $@

# 定义 伪目标
.PHONY: clean

clean:
	-rm -f $(objs)
	-rm -f *.o
	-rm -f $(bin_path)/*
	-rm -rf ${binAbPath}


# --------- for test ---------
main: main.o $(objs)
	gcc -o $(bin_path)/$@ $^

# $* 匹配 %，$^ 匹配 main_%.o $(objs)
main_%: main_%.o $(objs)
	gcc -o $(bin_path)/main_$* $^

# patsubst 按照规则替换
t%:
	make dir
	make $(patsubst %.c, %, $(shell ls main_$*_*))
